package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.service.ITrainPrepareDetailService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 教师备课资料内容 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2025-08-16
 */
@RestController
@RequestMapping("/api/trainPrepareDetail")
public class TrainPrepareDetailController {
    @Autowired
    private ITrainPrepareDetailService iTrainPrepareDetailService;


    @GetMapping("/trainPrepareDetailList")
    public Result trainPrepareDetailList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                          @RequestParam(value = "yearPart", required = false) String yearPart,
                          @RequestParam(value = "quarterNum", required = false) String quarterNum,
                          @RequestParam(value = "yearClassId", required = false) String yearClassId,
                          @RequestParam(value = "subjectsId", required = false) String subjectsId,
                          @RequestParam(value = "classTypeId", required = false) String classTypeId,
                          @RequestParam(value = "prepareId", required = false) String prepareId,
                          @RequestParam(value = "lessonNum", required = false) String lessonNum
                          ){
        IPage<TrainPrepareDetail> iPage = new Page<>(pageNum, pageSize);
        QueryWrapper<TrainPrepareDetail> qw = new QueryWrapper<>();
        qw.eq("prepare_id", prepareId);
        qw.eq(StringUtils.isNotBlank(lessonNum), "lesson_num", lessonNum);
        qw.eq("deleted", 0);
        qw.orderByAsc("lesson_num", "shutnum_num");
        iTrainPrepareDetailService.page(iPage, qw);
        for (TrainPrepareDetail trainPrepareDetail : iPage.getRecords()){
            trainPrepareDetail.setIsContentStatus(1);
            if (trainPrepareDetail.getContent() == null || trainPrepareDetail.getContent().equals("")){
                trainPrepareDetail.setIsContentStatus(0);
            }
        }
        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }

    @PostMapping("/addTrainPrepareDetail")
    public Result addTrainPrepareDetail(@RequestBody TrainPrepareDetail trainPrepareDetail) {
        QueryWrapper<TrainPrepareDetail> qw = new QueryWrapper<>();
        qw.eq("prepare_id", trainPrepareDetail.getPrepareId());
        qw.eq("lesson_num", trainPrepareDetail.getLessonNum());
//        qw.eq("shutnum_num", trainPrepareDetail.getShutnumNum());
        long count = iTrainPrepareDetailService.count(qw);
        if (count > 0) {
            return new Result(Code.ERROR, null, "1个课次只能上传1关，已上传过1关，不能再上传，上传失败");
        }

        boolean save = iTrainPrepareDetailService.save(trainPrepareDetail);
        if (!save) {
            return new Result(Code.ERROR, null, "添加失败");
        }
        return new Result(Code.OK, null, "添加成功");
    }

    @PutMapping("/editTrainPrepareDetail")
    public Result editTrainPrepareDetail(@RequestBody TrainPrepareDetail trainPrepareDetail) {
        TrainPrepareDetail trainPrepareDetailOld = iTrainPrepareDetailService.getById(trainPrepareDetail.getId());
        if (Objects.isNull(trainPrepareDetailOld)){
            return new Result(Code.ERROR, null, "该数据不存在,编辑失败");
        }
        QueryWrapper<TrainPrepareDetail> qw = new QueryWrapper<>();
        qw.ne("id", trainPrepareDetail.getId());
        qw.eq("prepare_id", trainPrepareDetail.getPrepareId());
        qw.eq("lesson_num", trainPrepareDetail.getLessonNum());
//        qw.eq("shutnum_num", trainPrepareDetail.getShutnumNum());
        long count = iTrainPrepareDetailService.count(qw);
        if (count > 0) {
            return new Result(Code.ERROR, null, "1个课次只能上传1关，已上传过1关，不能再上传，上传失败");
        }
        boolean update = iTrainPrepareDetailService.updateById(trainPrepareDetail);
        if (!update) {
            return new Result(Code.ERROR, null, "修改失败");
        }
        return new Result(Code.OK, null, "修改成功");
    }

    @DeleteMapping("/deleteTrainPrepareDetail")
    public Result deleteTrainPrepareDetail(@RequestBody Map reMap){
        List<String> ids = (List<String>) reMap.get("ids");
        if (ids == null || ids.isEmpty()){
            return new Result(Code.DELETE_ERROR, "", "请选择要删除的id");
        }
        QueryWrapper<TrainPrepareDetail> qw = new QueryWrapper<>();
        qw.in("id",ids);
        boolean remove = iTrainPrepareDetailService.remove(qw);
        if (remove) {
            return new Result(Code.OK, null, Code.DELETE_OK_MSG);
        }
        return new Result(Code.ERROR, null, Code.DELETE_ERROR_MSG);
    }



    @DeleteMapping("/deleteContent")
    public Result deleteContent(@RequestParam("id") String id){
        TrainPrepareDetail trainPrepareDetailOld = iTrainPrepareDetailService.getById(id);
        if (Objects.isNull(trainPrepareDetailOld)){
            return new Result(Code.ERROR, null, "该数据不存在");
        }
        trainPrepareDetailOld.setContent("");
        trainPrepareDetailOld.setFileLink("");
        trainPrepareDetailOld.setFileName("");
        boolean update = iTrainPrepareDetailService.updateById(trainPrepareDetailOld);
        if (!update){
            return new Result(Code.ERROR, null, "删除失败");
        }
        return new Result(Code.OK, null, "删除成功");

    }


    @GetMapping("/getTrainPrepareDetailById")
    public Result getTrainPrepareDetailById(@RequestParam("id") String id){
        TrainPrepareDetail trainPrepareDetail = iTrainPrepareDetailService.getById(id);
        if (Objects.isNull(trainPrepareDetail)){
            return new Result(Code.ERROR, null, "该数据不存在");
        }
        return new Result(Code.OK, trainPrepareDetail, "查询成功");
    }

}
